export function getToday() {
  const current = new Date()
  const day = current.getDay()
  const year = current.getFullYear()
  let month = current.getMonth()
  let d = current.getDate()
  let week
  switch (day) {
      case 0:
          week = "星期日";
          break;
      case 1:
          week = "星期一";
          break;
      case 2:
          week = "星期二";
          break;
      case 3:
          week = "星期三";
          break;
      case 4:
          week = "星期四";
          break;
      case 5:
          week = "星期五";
          break;
      case 6:
          week = "星期六";
          break;
      default:
          week = "系统错误，无法读取日期！";
  }
  month = month + 1 >= 13 ? 12 : month + 1
  if (month < 10) {
    month = '0' + month
  }
  if (d < 10) {
    d = '0' + d
  }
  const today = {
      calendar: `${year}-${month}-${d}`,
      week
  }
  return today
}

//随机生成验证码
export function createCode() {
	let code = "";
	var codeLength = 5; //验证码的长度   
	var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
    's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
		'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); //随机数   
	for(var i = 0; i < codeLength; i++) { //循环操作   
		var charIndex = Math.floor(Math.random() * 36); //取得随机数的索引   
		code += random[charIndex]; //根据索引取得随机数加到code上   
    }
    return code
 }

 export const setStore = (name, value) => {
    if (!name || !value || value === '') return;
    if (typeof value !== 'string') {
        value = JSON.stringify(value)
    }
    window.localStorage.setItem(name, value);
}

export const getStore = (name) => {
    if (!name) return;
    return window.localStorage.getItem(name)
}

export const removeStore = (name) => {
    if (!name) return;
    window.localStorage.removeItem(name)
}


export const backTop = () => {
    var timer = null
    cancelAnimationFrame(timer);
    timer = requestAnimationFrame(function fn(){
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
            window.scrollBy(0, -200);
            timer = requestAnimationFrame(fn);
        } else {
            cancelAnimationFrame(timer);
        }    
    })
  }

export const setArray = (baseArray) => {
    let len = baseArray.length;
    let n = 4
    let lineNum = len % n === 0 ? len / n : Math.floor( (len / n) + 1 );
    let res = []
    for (let i = 0; i < lineNum; i++) {
        let temp = baseArray.slice(i*n, i*n+n);
        res.push(temp);
    }
    return res
}


export function IEVersion() {
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串  
    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器  
    return isIE
    // var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器  
    // var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
    // if (isIE) {
    //     var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
    //     reIE.test(userAgent);
    //     var fIEVersion = parseFloat(RegExp["$1"]);
    //     if(fIEVersion == 7) {
    //         return 7;
    //     } else if (fIEVersion == 8) {
    //         return 8;
    //     } else if (fIEVersion == 9) {
    //         return 9;
    //     } else if (fIEVersion == 10) {
    //         return 10;
    //     } else {
    //         return 6;//IE版本<=7
    //     }
    // }
    //  else if (isEdge) {
    //     return 'edge';//edge
    // } else if (isIE11) {
    //     return 11; //IE11  
    // } else {
    //     return -1;//不是ie浏览器
    // }
}
